import { Router } from 'express'
import { ObjectId } from 'mongodb'
import '../utils/database/mongodb/database'
import _ from 'lodash'

const router = Router()
const { DataBase } = require('../utils/database/mongodb/database')
const db = new DataBase()
db.initMongoDB()
db.setDatabase('yd')

const msg = require('../msg/message.js')

let userList = []

router.get('/online/users', function (req, res, next) {
  console.log('online users')
  console.log(req.sessionId)
  res.json(userList)
})
router.get('/online/add/users', function (req, res, next) {
  console.log('online users')
  userList.push(req.query.name)
  res.json(userList)
})

// 取用户名列表
router.get('/users/names', function (req, res, next) {
  console.log('find users')
  db.findAll('police', doc => {
    const names = []
    _.each(doc, obj => {
      names.push(obj.username)
    })
    res.json(names)
  })
})

// 登陆
router.post('/users/login', (req, res) => {
  console.log(req.body)
  db.findOne('police', { username: req.body.username }, dataObject => {
    console.log(dataObject)
    if (dataObject === undefined) {
      res.status(401).json({
        message: '用户不存在'
      })
      return
    }
    if (req.body.username === dataObject.username && req.body.password === dataObject.password) {
      console.log('登录成功')
      db.findOne('unit', { _id: ObjectId(dataObject.unitId) }, doc => {
        dataObject = _.assign(dataObject, { unit: doc })
        req.session.authUser = dataObject
        return res.json(dataObject)
      })
    } else {
      res.status(401).json({
        message: '密码错误'
      })
    }
  })
})

// Add POST - /api/users/logout
router.post('/users/logout', (req, res) => {
  delete req.session.authUser
  res.json({ ok: true })
})

// 注册
router.post('/users/register', (req, res) => {
  console.log('用户名：' + req.body.username)
  db.findOne('users', { username: req.body.username }, doc => {
    console.log(doc)
    if (doc !== undefined) {
      res.json({
        message: '用户已经存在'
      })
    } else {
      let user = {
        userGroup: 'Guest',
        auth: false
      }
      user.registerTime = new Date().toLocaleString()
      Object.assign(user, req.body)
      db.insert('users', user, doc => {
        console.log('注册成功')
        console.log(doc.ops)
        res.json(doc)
      })
    }
  })
})

// 报名
router.post('/users/bm', (req, res) => {
  db.insert('baoming', req.body, doc => {
    console.log('注册成功')
    console.log(doc)
    res.json(doc)
  })
})

/* GET users listing. */
router.get('/users', function (req, res, next) {
  console.log('find users')
  db.findAll('users', doc => {
    res.json(doc)
  })
})

// router.get('/users/:id', function (req, res, next) {
//   const id = parseInt(req.params.id)
//   if (id >= 0 && id < db.findAll().length) {
//     res.json(db.findAll()[id])
//   } else {
//     res.sendStatus(404)
//   }
// })

/* GET user by username. */
router.get('/users/:username', function (req, res, next) {
  db.findOne('users', { username: req.params.username }, doc => {
    if (!doc) {
      res.json(msg.userNoFound)
    } else {
      res.json(doc)
    }
  })
})

/* check username. */
router.get('/users/check/:username', function (req, res, next) {
  db.findOne('police', { username: req.params.username }, doc => {
    if (!doc) {
      res.json(msg.userNoFound)
    } else {
      res.json(msg.userFound)
    }
  })
})

/* check username. */
router.get('/users/checkEmail/:email', function (req, res, next) {
  db.findOne('users', { email: req.params.email }, doc => {
    if (!doc) {
      res.json(msg.userNoFound)
    } else {
      res.json(msg.emailIsExisted)
    }
  })
})

export default router
